//<template>
  <div class="ShopList">
    <el-button @click="ShopListAdd()">添加</el-button>
    <template>
      <el-table :data="tableData" style="width: 100%; height: 100%">
        <el-table-column
          v-for="(item, index) in tableList"
          :prop="item.prop"
          :label="item.label"
          :key="index"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 弹框 -->
    <el-dialog
      :title="titles"
      :visible.sync="dialogVisible"
      width="30%"
      @click="dialogVisible = false"
    >
      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="店铺名称" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="city">
          <el-input v-model="ruleForm.city"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tal">
          <el-input v-model.number="ruleForm.tal"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="shoplistSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from "vuex";

export default {
  data() {
    return {
      titles: "编辑",
      dialogVisible: false,
      ruleForm: {
        id: "",
        title: "",
        city: "",
        tal: "",
      },
    };
  },
  created() {
    // 默认调取仓库的获取数据
    this.$store.dispatch("asyncgettableData");
  },
  computed: {
    // 直接当做普通属性调用不加括号
    // 任何data中数据变化立即重新计算
    // 计算属性会缓存
    // return 必须retrun结果
    // 获取仓库的数据
    ...mapState(["tableData", "tableList"]),
  },
  mounted() {},
  methods: {
    // 新增按钮
    ShopListAdd() {
      this.titles = "新增";
      this.dialogVisible = true;
    },
    // 编辑按钮
    handleEdit(index, row) {
      this.titles = "编辑";
      this.dialogVisible = true;
      // console.log(index, row);
      this.ruleForm.id = row.id;
      this.ruleForm.title = row.title;
      this.ruleForm.city = row.city;
      this.ruleForm.tal = row.tal;
    },
    // 点击确定后 —— 更新编辑或新增数据
    shoplistSubmit() {
      if (this.titles == "编辑") {
        this.$store.commit("shoplistEdit", this.ruleForm);
      } else if (this.titles == "新增") {
        this.ruleForm.id = new Date() * 1;
        this.$store.commit("shoplistAdd", this.ruleForm);
      }
      this.dialogVisible = false;
    },
    // 删除按钮
    handleDelete(index, row) {
      this.$store.commit("shoplistDel", row.id);
    },
  },
};
</script>

<style lang="scss" scoped>
.ShopList {
  width: 100%;
  height: 100%;
}
</style>